﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Navigation;

namespace Nok_Air
{
    public partial class SelectFlights : PhoneApplicationPage
    {
        private string PassengerInfoPar = "";
        private string roundtripFlag = "";
        private int[] indexAvailabilityItems;
        private string[] sellingClass_indexAvailabilityItems;
        public SelectFlights()
        {
            InitializeComponent();
            this.DataContext = App.ViewModel;
        }

        private void continueButton_Click(object sender, RoutedEventArgs e)
        {
            string indexAvailabilityItemsPar = "";
            foreach (int _value in indexAvailabilityItems)
            {
                if (_value == -1)
                {
                    MessageBox.Show("Please select Flight");
                    return;
                }
            }

            //default is Oneway=> 0==oneway,1==roundTrip
            if (roundtripFlag == "0")
            {
                indexAvailabilityItemsPar = indexAvailabilityItems[0].ToString() == "-1" ? "" : indexAvailabilityItems[0].ToString() + "_" + sellingClass_indexAvailabilityItems[0];
            }
            else
            {
                indexAvailabilityItemsPar = indexAvailabilityItems[0].ToString() == "-1" ? "" : indexAvailabilityItems[0].ToString() + "_" + sellingClass_indexAvailabilityItems[0];
                indexAvailabilityItemsPar += indexAvailabilityItems[1].ToString() == "-1" ? "" : "," + indexAvailabilityItems[1].ToString() + "_" + sellingClass_indexAvailabilityItems[1];
            }
            NavigationService.Navigate(new Uri("/BookFlight/3Payment.xaml?indexAvailabilityItems=" + indexAvailabilityItemsPar + "&PassengerInfo=" + PassengerInfoPar + "&roundtripFlag=" + roundtripFlag, UriKind.Relative));
        }

        private Microsoft.Phone.Controls.Primitives.PivotHeaderItem pivotHItem = null;
        private System.Windows.Controls.ListBox listBox = null;
        private string LoopThroughControls(UIElement parent)
        {
            int count = VisualTreeHelper.GetChildrenCount(parent);
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    UIElement child = (UIElement)VisualTreeHelper.GetChild(parent, i);
                    string childTypeName = child.GetType().ToString();


                    System.Diagnostics.Debug.WriteLine(childTypeName);
                    if (childTypeName.Equals("Microsoft.Phone.Controls.Primitives.PivotHeaderItem"))
                    {
                        //  MessageBox.Show(child.GetType().ToString());
                        pivotHItem = child as Microsoft.Phone.Controls.Primitives.PivotHeaderItem;
                        LoopThroughControls(pivotHItem);
                    }
                    else if (pivotHItem != null && childTypeName.Equals("System.Windows.Controls.ContentPresenter"))
                    {

                        TextBlock tBlock = VisualTreeHelper.GetChild(child, 0) as TextBlock;
                        //  MessageBox.Show(VisualTreeHelper.GetChild(child, 0).GetType().ToString());
                        MessageBox.Show(tBlock.Text);
                        pivotHItem = null;
                    }

                    if (childTypeName.Equals("System.Windows.Controls.ListBox"))
                    {
                        listBox = child as System.Windows.Controls.ListBox;
                        MessageBox.Show(listBox.Tag.ToString());
                        LoopThroughControls(listBox);
                    }

                    else if (listBox != null && childTypeName.Equals("System.Windows.Controls.ListBoxItem"))
                    {

                    }
                    //   TextBlock tBlock = VisualTreeHelper.GetChild(child, 0) as TextBlock;
                    //  MessageBox.Show(VisualTreeHelper.GetChild(child, 0).GetType().ToString());
                    //     MessageBox.Show(tBlock.Text);
                    //   MessageBox.Show(listBox.ItemsSource.ToString()); 

                    LoopThroughControls(child);
                }
            }
            return count.ToString();
        }

        private void ClassEconomy_Checked(object sender, RoutedEventArgs e)
        {
            int indexListBoxItem = -1;
            int indexPivotItem = -1;
            Grid grid = (Grid)VisualTreeHelper.GetParent(sender as RadioButton);
            ListBoxItem listBoxItem = null;
            DependencyObject parentListBoxItem = VisualTreeHelper.GetParent(grid);
            while (parentListBoxItem != null)
            {
                listBoxItem = parentListBoxItem as ListBoxItem;
                if (listBoxItem != null)
                {
                    break;
                }
                // Get the next parent.
                parentListBoxItem = VisualTreeHelper.GetParent(parentListBoxItem);
            }

            if (listBoxItem != null)
            {
                ListBox listBox = null;
                DependencyObject parentListBox = VisualTreeHelper.GetParent(listBoxItem);
                while (parentListBox != null)
                {
                    listBox = parentListBox as ListBox;
                    if (listBox != null)
                    {
                        object item = listBoxItem.DataContext;
                        indexListBoxItem = listBox.Items.IndexOf(item);
                        //   MessageBox.Show(index.ToString());
                        break;
                    }
                    // Get the next parent.
                    parentListBox = VisualTreeHelper.GetParent(parentListBox);
                }

                if (listBox != null)
                {
                    Microsoft.Phone.Controls.PivotItem pivotItem = null;
                    DependencyObject parentpivotItem = VisualTreeHelper.GetParent(listBox);
                    while (parentpivotItem != null)
                    {
                        pivotItem = parentpivotItem as Microsoft.Phone.Controls.PivotItem;
                        if (pivotItem != null)
                        {
                            break;
                        }
                        // Get the next parent.
                        parentpivotItem = VisualTreeHelper.GetParent(parentpivotItem);
                    }

                    if (pivotItem != null)
                    {
                        Microsoft.Phone.Controls.Pivot pivot = null;
                        DependencyObject parentpivot = VisualTreeHelper.GetParent(pivotItem);
                        while (parentpivot != null)
                        {
                            pivot = parentpivot as Microsoft.Phone.Controls.Pivot;
                            if (pivot != null)
                            {
                                object item = pivotItem.DataContext;

                                indexPivotItem = pivot.Items.IndexOf(item);

                                indexAvailabilityItems[indexPivotItem] = indexListBoxItem;
                                // MessageBox.Show("pivot index = " + index.ToString());
                                break;
                            }
                            // Get the next parent.
                            parentpivot = VisualTreeHelper.GetParent(parentpivot);
                        }

                        if (indexPivotItem != -1 && indexListBoxItem != -1)
                        {
                            sellingClass_indexAvailabilityItems[indexPivotItem] = App.ViewModel.AvailabilityItems[indexPivotItem].FlightSegmentItem[indexListBoxItem].ClassEconomy.ClassName;
                            string typeStr = null;
                            string fareValStr = App.ViewModel.AvailabilityItems[indexPivotItem].FlightSegmentItem[indexListBoxItem].ClassEconomy.FareValue;
                            TextBlock txtBlock = null;
                            if (indexPivotItem == 0)
                            {
                                typeStr = App.ViewModel.Depart2 + " " + App.ViewModel.AvailabilityItems[indexPivotItem].FlightSegmentItem[indexListBoxItem].ClassEconomy.ClassCode;
                                txtBlock = summaryDepTBlock;
                            }
                            else
                            {
                                typeStr = App.ViewModel.Return2 + " " + App.ViewModel.AvailabilityItems[indexPivotItem].FlightSegmentItem[indexListBoxItem].ClassEconomy.ClassCode;
                                txtBlock = summaryRetTBlock;
                            }

                            txtBlock.Text = string.Format("{0}: " + App.ViewModel.AvailabilityItems[indexPivotItem].FlightSegmentItem[indexListBoxItem].FlightNumber + "  " +
                               App.ViewModel.AvailabilityItems[indexPivotItem].FlightSegmentItem[indexListBoxItem].DepartureTime + "-" +
                               App.ViewModel.AvailabilityItems[indexPivotItem].FlightSegmentItem[indexListBoxItem].ArrivalTime + "  " +
                               "{1} " + App.ViewModel.MoneyUnit, typeStr, fareValStr);
                        }
                    }
                }
            }
            showSummary.Begin();
        }
        private void ClassFlexi_Checked(object sender, RoutedEventArgs e)
        {
            int indexListBoxItem = -1;
            int indexPivotItem = -1;
            Grid grid = (Grid)VisualTreeHelper.GetParent(sender as RadioButton);
            ListBoxItem listBoxItem = null;
            DependencyObject parentListBoxItem = VisualTreeHelper.GetParent(grid);
            while (parentListBoxItem != null)
            {
                listBoxItem = parentListBoxItem as ListBoxItem;
                if (listBoxItem != null)
                {
                    break;
                }
                // Get the next parent.
                parentListBoxItem = VisualTreeHelper.GetParent(parentListBoxItem);
            }

            if (listBoxItem != null)
            {
                ListBox listBox = null;
                DependencyObject parentListBox = VisualTreeHelper.GetParent(listBoxItem);
                while (parentListBox != null)
                {
                    listBox = parentListBox as ListBox;
                    if (listBox != null)
                    {
                        object item = listBoxItem.DataContext;
                        indexListBoxItem = listBox.Items.IndexOf(item);
                        //   MessageBox.Show(index.ToString());
                        break;
                    }
                    // Get the next parent.
                    parentListBox = VisualTreeHelper.GetParent(parentListBox);
                }

                if (listBox != null)
                {
                    Microsoft.Phone.Controls.PivotItem pivotItem = null;
                    DependencyObject parentpivotItem = VisualTreeHelper.GetParent(listBox);
                    while (parentpivotItem != null)
                    {
                        pivotItem = parentpivotItem as Microsoft.Phone.Controls.PivotItem;
                        if (pivotItem != null)
                        {
                            break;
                        }
                        // Get the next parent.
                        parentpivotItem = VisualTreeHelper.GetParent(parentpivotItem);
                    }

                    if (pivotItem != null)
                    {
                        Microsoft.Phone.Controls.Pivot pivot = null;
                        DependencyObject parentpivot = VisualTreeHelper.GetParent(pivotItem);
                        while (parentpivot != null)
                        {
                            pivot = parentpivot as Microsoft.Phone.Controls.Pivot;
                            if (pivot != null)
                            {
                                object item = pivotItem.DataContext;

                                indexPivotItem = pivot.Items.IndexOf(item);

                                indexAvailabilityItems[indexPivotItem] = indexListBoxItem;
                                // MessageBox.Show("pivot index = " + index.ToString());
                                break;
                            }
                            // Get the next parent.
                            parentpivot = VisualTreeHelper.GetParent(parentpivot);
                        }

                        if (indexPivotItem != -1 && indexListBoxItem != -1)
                        {
                            sellingClass_indexAvailabilityItems[indexPivotItem] = App.ViewModel.AvailabilityItems[indexPivotItem].FlightSegmentItem[indexListBoxItem].ClassFlexi.ClassName;
                            string typeStr = null;
                            string fareValStr = App.ViewModel.AvailabilityItems[indexPivotItem].FlightSegmentItem[indexListBoxItem].ClassFlexi.FareValue;
                            TextBlock txtBlock = null;
                            if (indexPivotItem == 0)
                            {
                                typeStr = App.ViewModel.Depart2 + " " + App.ViewModel.AvailabilityItems[indexPivotItem].FlightSegmentItem[indexListBoxItem].ClassFlexi.ClassCode;
                                txtBlock = summaryDepTBlock;
                            }
                            else
                            {
                                typeStr = App.ViewModel.Return2 + " " + App.ViewModel.AvailabilityItems[indexPivotItem].FlightSegmentItem[indexListBoxItem].ClassFlexi.ClassCode;
                                txtBlock = summaryRetTBlock;
                            }

                            txtBlock.Text = string.Format("{0}: " + App.ViewModel.AvailabilityItems[indexPivotItem].FlightSegmentItem[indexListBoxItem].FlightNumber + "  " +
                               App.ViewModel.AvailabilityItems[indexPivotItem].FlightSegmentItem[indexListBoxItem].DepartureTime + "-" +
                               App.ViewModel.AvailabilityItems[indexPivotItem].FlightSegmentItem[indexListBoxItem].ArrivalTime + "  " +
                               "{1} " + App.ViewModel.MoneyUnit, typeStr, fareValStr);
                        }
                    }
                }
            }
            showSummary.Begin();
        }
        private void ClassNokPlus_Checked(object sender, RoutedEventArgs e)
        {
            int indexListBoxItem = -1;
            int indexPivotItem = -1;
            Grid grid = (Grid)VisualTreeHelper.GetParent(sender as RadioButton);
            ListBoxItem listBoxItem = null;
            DependencyObject parentListBoxItem = VisualTreeHelper.GetParent(grid);
            while (parentListBoxItem != null)
            {
                listBoxItem = parentListBoxItem as ListBoxItem;
                if (listBoxItem != null)
                {
                    break;
                }
                // Get the next parent.
                parentListBoxItem = VisualTreeHelper.GetParent(parentListBoxItem);
            }

            if (listBoxItem != null)
            {
                ListBox listBox = null;
                DependencyObject parentListBox = VisualTreeHelper.GetParent(listBoxItem);
                while (parentListBox != null)
                {
                    listBox = parentListBox as ListBox;
                    if (listBox != null)
                    {
                        object item = listBoxItem.DataContext;
                        indexListBoxItem = listBox.Items.IndexOf(item);
                        //   MessageBox.Show(index.ToString());
                        break;
                    }
                    // Get the next parent.
                    parentListBox = VisualTreeHelper.GetParent(parentListBox);
                }

                if (listBox != null)
                {
                    Microsoft.Phone.Controls.PivotItem pivotItem = null;
                    DependencyObject parentpivotItem = VisualTreeHelper.GetParent(listBox);
                    while (parentpivotItem != null)
                    {
                        pivotItem = parentpivotItem as Microsoft.Phone.Controls.PivotItem;
                        if (pivotItem != null)
                        {
                            break;
                        }
                        // Get the next parent.
                        parentpivotItem = VisualTreeHelper.GetParent(parentpivotItem);
                    }

                    if (pivotItem != null)
                    {
                        Microsoft.Phone.Controls.Pivot pivot = null;
                        DependencyObject parentpivot = VisualTreeHelper.GetParent(pivotItem);
                        while (parentpivot != null)
                        {
                            pivot = parentpivot as Microsoft.Phone.Controls.Pivot;
                            if (pivot != null)
                            {
                                object item = pivotItem.DataContext;

                                indexPivotItem = pivot.Items.IndexOf(item);

                                indexAvailabilityItems[indexPivotItem] = indexListBoxItem;
                                // MessageBox.Show("pivot index = " + index.ToString());
                                break;
                            }
                            // Get the next parent.
                            parentpivot = VisualTreeHelper.GetParent(parentpivot);
                        }

                        if (indexPivotItem != -1 && indexListBoxItem != -1)
                        {
                            sellingClass_indexAvailabilityItems[indexPivotItem] = App.ViewModel.AvailabilityItems[indexPivotItem].FlightSegmentItem[indexListBoxItem].ClassNokPlus.ClassName;
                            string typeStr = null;
                            string fareValStr = App.ViewModel.AvailabilityItems[indexPivotItem].FlightSegmentItem[indexListBoxItem].ClassNokPlus.FareValue;
                            TextBlock txtBlock = null;
                            if (indexPivotItem == 0)
                            {
                                typeStr = App.ViewModel.Depart2 + " " + App.ViewModel.AvailabilityItems[indexPivotItem].FlightSegmentItem[indexListBoxItem].ClassNokPlus.ClassCode;
                                txtBlock = summaryDepTBlock;
                            }
                            else
                            {
                                typeStr = App.ViewModel.Return2 + " " + App.ViewModel.AvailabilityItems[indexPivotItem].FlightSegmentItem[indexListBoxItem].ClassNokPlus.ClassCode;
                                txtBlock = summaryRetTBlock;
                            }

                            txtBlock.Text = string.Format("{0}: " + App.ViewModel.AvailabilityItems[indexPivotItem].FlightSegmentItem[indexListBoxItem].FlightNumber + "  " +
                               App.ViewModel.AvailabilityItems[indexPivotItem].FlightSegmentItem[indexListBoxItem].DepartureTime + "-" +
                               App.ViewModel.AvailabilityItems[indexPivotItem].FlightSegmentItem[indexListBoxItem].ArrivalTime + "  " +
                               "{1} " + App.ViewModel.MoneyUnit, typeStr, fareValStr);
                        }
                    }
                }
            }
            showSummary.Begin();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            if (NavigationService.CanGoBack && App.ViewModel.IsFinishTransaction)
            {
                NavigationService.GoBack();
            }

            if (NavigationContext.QueryString.TryGetValue("roundtripFlag", out roundtripFlag) && NavigationContext.QueryString.TryGetValue("PassengerInfo", out PassengerInfoPar))
            {
                //default is Oneway=> 0==oneway,1==roundTrip
                if (roundtripFlag == "0")
                {
                    indexAvailabilityItems = new int[1];
                    sellingClass_indexAvailabilityItems = new string[1];
                }
                else
                {
                    indexAvailabilityItems = new int[2];
                    sellingClass_indexAvailabilityItems = new string[2];
                }
                for (int aitemIndex = 0; aitemIndex < indexAvailabilityItems.Count(); aitemIndex++)
                {
                    indexAvailabilityItems[aitemIndex] = -1;
                    sellingClass_indexAvailabilityItems[aitemIndex] = "-1";
                }
            }
        }
    }
    /*
    public class SoldOutColorConverter : System.Windows.Data.IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string farevalue = value as string;
            string defaultcolor = parameter as string;
            string red = defaultcolor.Substring(0,2);
            string green = defaultcolor.Substring(2,2);
            string blue = defaultcolor.Substring(4,2);
            if(farevalue.Equals("SOLD OUT"))
            {
                return Colors.Orange;
            }
            else
            {
                return new Color()
                {
                    R = byte.Parse(red,System.Globalization.NumberStyles.AllowHexSpecifier),
                    G = byte.Parse(red,System.Globalization.NumberStyles.AllowHexSpecifier),
                    B = byte.Parse(red,System.Globalization.NumberStyles.AllowHexSpecifier)
                };
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Color input = (Color)value;
            return string.Concat(input.A, input.R, input.G, input.B);
        }
    }*/
}